main.controller('loginCtrl', ['$scope','tools','$cookies','$timeout','Session','$location','SweetAlert','NoticeServ','$http','$rootScope', function ($scope, tools,$cookies,$timeout,Session,$location,SweetAlert,NoticeServ,$http,$rootScope) {

	if($(window).width()<=414){
		$scope.mobile = true;
	}else{
		$scope.mobile = false;
	}

	$rootScope.userInfo = {};

	$scope.User = {
		oldName:'',
		name:'',
		password:'',
		remember:false,
		rememberStuff:false,
		nameError:'',
		passwordError:'',
		init:function(){
			var data = $cookies.get('user');
			console.log(data);
			if(data){
				data=JSON.parse(data);
				this.name = data.name;
				this.password = data.password;
				this.remember = data.remember; 
				this.rememberStuff = true;
			}
		},
		isRemember:function(){			
			
			console.log(this.remember);
			if(this.remember){
				var data = {
					name:this.name,
					password:this.password,
					remember:true
				};

				$cookies.put('user',JSON.stringify(data));
				console.log($cookies.get('user'));
			}else{
				console.log('remove cookie');
				$cookies.remove('user');
			}
			
		},
		checkName:function(){
			
		},
		login:function(){

			if(this.nameError !='' || this.name == '' || this.password == ''){
				return;
			}
			var $this = this;
			console.log(this.remember);
			tools.$get({
				url:'/marketconditions/user/userLogin.htm',
				data:{
					phone:this.name,
					password:this.password
				}
			}).then(function(resp){
				console.log('++++')
				console.log(resp);
				$this.passwordError = "";
				$this.isRemember();
				Session.create(resp.value);
				$rootScope.userInfo = resp.value
				$location.path('/dashBroad');
				NoticeServ.$broadcast('LOGIN_EV');
				// SweetAlert.swal({
				// 	title: "登录成功",
				// 	type: "success",
				// 	showCancelButton: false,
				// 	confirmButtonColor: "#DD6B55",
				// 	confirmButtonText: "去首页",
				// 	closeOnConfirm: true
				// },function(){ 
				// 	$timeout(function(){
				// 		$location.path('/share');
				// 		$rootScope.$broadcast('LOGIN_SUCCESS_EV');
				// 	},100);
				// });
			
			},function(errResp){
				console.log(errResp);


				if(errResp.message == "alreadyEnter"){
					SweetAlert.swal('','该用户已登录','error');
				}else{
					$this.passwordError = "密码错误";
					SweetAlert.swal('','账号或密码错误','error');
				}

				
				// console.log(errResp)
				console.log('error');
			});
		}
	};

	$scope.User.init();
}]);
